(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.site.updateRejectController', updateRejectController);
	updateRejectController.$inject = ['$q', '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'siteService' ];
	function updateRejectController($q, $scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, siteService) {
		var ctrl = this, siteInfo = $scope.$stateParams.params, errorMessage = {};
		//ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.authService = authService;
		ctrl.site = {};
		siteService.initPageCallback(ctrl);
		siteService.initTableConfig($scope, ctrl, layoutConfig);
		ctrl.$onInit = function() {
			var site, layout, siteOptions, localeCodeOptions, promise, promises = [];
			promise = authService.updateReject(siteInfo);
			promises.push(promise);
			
			promise = siteService.getLayoutConfig('updateReject');
			promises.push(promise);
			
			promise = siteService.searchEvents(ctrl, ctrl.eventSearchCriteria);
			promises.push(promise);
			
			promise = siteService.loadSiteOptions();
			promises.push(promise);
			
			promise = siteService.getLocaleOptions();
			promises.push(promise);
			
			$q.all(promises).then(function(response) {
				site = response[0].data.model;
				site.taskId = response[0].data.task.taskId;
				site.localeCodes = site.localeCodes || [];
				layout = response[1].data ? response[1].data : response[1];
				site.eventSearchCriteria = response[2];
				siteOptions = response[3];
				localeCodeOptions = response[4];
				if (layout && site) {
					siteService.initSiteOptionData(ctrl, siteOptions);
					siteService.initReport($scope, ctrl, true);
					siteService.initEdiProfile(site);
					siteService.resetDynaProperties(ctrl, layout, site);
					siteService.initSiteUserTheme(site, ctrl);
					ctrl.layoutConfig = layout;
					ctrl.site = site;
					siteService.setLocaleOptions(ctrl);
					ctrl.site.siteReports = ctrl.site.siteReports || [];
					angular.forEach(ctrl.site.siteReports, function(siteReport, key) {
						if (siteReport && siteReport.dataStatus != "V") {
							ctrl.siteReport.serviceCode = siteReport.report.serviceCode;
						}
						if (!ctrl.site['siteReports' + siteReport.report.serviceCode]) {								
							ctrl.site['siteReports' + siteReport.report.serviceCode] = [];
						}
						ctrl.site['siteReports' + siteReport.report.serviceCode].push(siteReport);
					});
				}
			});
		};
		
		ctrl.addCompany = function (){
			return $q(function(resolved, rejected) {
				var filterCompanyCodes = [];
				ctrl.site.company.subCompanies = ctrl.site.company.subCompanies || [];
				for (var i = 0; i < ctrl.site.company.subCompanies.length; i++) {
					filterCompanyCodes.push(ctrl.site.company.subCompanies[i].companyCode);
				}
				modalService.openCustomDialogs({
					'title' : 'Company Lookup',
					'dialogsModule' : 'cda.company',
					'params' : {
						companyCode : ctrl.site.siteCode,
						action : 'UPDATE',
						filterCompanyCodes : filterCompanyCodes,
						lookupFlag : "Y",
						companyType : (storageService.getUserInfo().siteCode == 'SYSADM') ? storageService.getUserInfo().companyType : "",
						companyTypeReadOnlyForBank : storageService.getUserInfo().siteCode
					}, // add for not sysadm cooperation
					'btnCallback' : {
						'ok' : function(subCompanies) {
							if (!subCompanies || subCompanies.length == 0) {
								modalService.openErrorDialogs({
									messages : [ 'title.dialogs.info.norecsel' ]
								});
								return false;
							}
							var subcompany = subCompanies[0];
							if (!ctrl.site.company.subCompanies) {
								ctrl.site.company.subCompanies = [];
							}
							var exist = false;
							for (var i = 0; i < ctrl.site.company.subCompanies.length; i++) {
								var item = ctrl.site.company.subCompanies[i];
								if (item.companyCode == subcompany.companyCode) {
									exist = true;
									break;
								}
							}
							if (!exist) {
								subcompany.childCompanyCode = subcompany.companyCode;
								subcompany.dataStatus = 'C';
								ctrl.site.company.subCompanies.push(subcompany);
							}
						},
						'close' : function() {
							rejected();
						}
					}

				});
			});
		};

		ctrl.lookuplocaleCodes = function(){
			var passparams=[];
			if(ctrl.localeOptions && ctrl.localeOptions.data.length>0){
				siteService.getLocaleCodesTwo(ctrl).then(function(){
					for(var i=0; i<ctrl.localeOptions.data.length; i++){
						for(var j=0; j<ctrl.localeCodesData.data.length; j++){
							if(ctrl.localeOptions.data[i].value==ctrl.localeCodesData.data[j].value){
							    var a={};
							    a.localeCode=ctrl.localeCodesData.data[j].value;
								passparams.push(a);
							}
						}
					}
				});
			}
			popup(passparams);
		};
		
		function popup(parameter){
			modalService.openCustomDialogs({
				'dialogsModule' : 'cda.localeCodes.popupedit',
				'title' : 'title.dialogs.info',
				'params': parameter,
				'btnCallback' : {
					'ok' : function(localeCodes) {
						if (!localeCodes || localeCodes.length == 0) {
							modalService.openErrorDialogs({
								messages : [ 'title.dialogs.info.norecsel' ]
							});
						}
						ctrl.site.localeCodes=[];
						if(localeCodes!=null && localeCodes.length>0){
							for(var i=0; i<localeCodes.length; i++){
							    ctrl.site.localeCodes.push(localeCodes[i].localeCode);
							}
						}
						siteService.setLocaleOptions(ctrl);
						
				     }
				}
			});
		}
		
		ctrl.loadDynaparms = function (){
			var params = {};
			// for get dynaparm page
			params.glPostMgrDynaparm = {};
			if(ctrl.site.glPostMgrDynaparm){
				params.glPostMgrDynaparm.dynamicParams = ctrl.site.glPostMgrDynaparm.dynamicParams;
			}
			if( ctrl.site.glPostMgr){
				params.glPostMgr = ctrl.site.glPostMgr;
			}
			modalService.openCustomDialogs({
				   'title': 'GL Posting Dynaparms',
				   'dialogsModule' : 'cda.glPostMgr.popupedit',
				   'params': params,
				   'btnCallback' : {
						'ok' : function(resultList) {
							ctrl.site.glPostMgrDynaparm = {};
							ctrl.site.glPostMgrDynaparm = resultList;
						}
					}	
				});
		};
		ctrl.updateRejectSave = function() {
			ctrl.site.notifyTpls = ctrl.site.notifyTpls || [];
			if ( ctrl.site.notifyTpls != null && ctrl.site.notifyTpls.length >0) {
				angular.forEach(ctrl.site.notifyTpls, function ( notifyDoc, key ){
					if ( !notifyDoc.notifyMimeType) notifyDoc.notifyMimeType = 'text/plain';
				});
			}
			return authService.updateRejectSave(ctrl.site).then(saveSuccessful, saveFailed);
		};

		ctrl.updateRejectSubmit = function() {
			ctrl.site.notifyTpls = ctrl.site.notifyTpls || [];
			if ( ctrl.site.notifyTpls != null && ctrl.site.notifyTpls.length >0) {
				angular.forEach(ctrl.site.notifyTpls, function ( notifyDoc, key ){
					if ( !notifyDoc.notifyMimeType) notifyDoc.notifyMimeType = 'text/plain';
				});
			}
			return authService.updateRejectSubmit(ctrl.site).then(submitSuccessful, submitFailed);
		};
		
		ctrl.updateRejectCancel = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.cancel', args : [ctrl.site.primaryKey]} ],
				'btnCallback' : {
					'ok' : function() {
						authService.updateRejectCancel(ctrl.site).then(cancelSuccessful, cancelFailed);
					}
				}
			});
		};

		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.submit' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.site.list',
					'state' : 'cda.site.list',
					'params' : {}
				}]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function saveSuccessful(response) {
			modalService.openInfoDialogs({
				'messages' : [ 'title.dialog.successful.save' ]
			});
			if (response.data && response.data.model) {
				ctrl.site = response.data.model;
			}
			if (response.data.task && response.data.task.taskId) {
				authService.saveSuccessfulProcess(response.data.task).then(function(response) {
					var routerInfo = response.data || {}, state = routerInfo.processState;
					delete routerInfo.processState;
					$scope.go(state, routerInfo);
				}, angular.noop);
			}
		}

		function saveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
		function cancelSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.cancel' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.site.list',
					'state' : 'cda.site.list',
					'params' : {}
				}]
			});
		}

		function cancelFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);
